package com.wenhai.spring.event;

import java.util.EventListener;
import java.util.EventObject;
import java.util.Observable;
import java.util.Observer;

/**
 *
 * {@link java.util.Observable} 示例
 *
 *
 * @ClassName ObservableDemo
 * @Description
 * @Author 谢文海
 * @Date 2021/4/10 22:58
 * @Version 1.0
 **/
public class ObservableDemo {

    public static void main(String[] args) {
        MyObservable myObservable = new MyObservable();
        myObservable.addObserver(new MyObserver());
        myObservable.notifyObservers(new EventObject("hello, 谢文海"));
    }


    static class MyObservable extends Observable {
        @Override
        public void notifyObservers(Object arg) {
            super.setChanged();
            super.notifyObservers(arg);
        }
    }

    static class MyObserver implements Observer, EventListener {

        @Override
        public void update(Observable o, Object msg) {

            System.out.println("收到观察者消息 : " + msg);
        }
    }


}
